package com.example.wangandroid.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.activity.BaseActivity
import com.example.wangandroid.databinding.ActivityArticleBinding


@Route(path = "/app/article")
open class ArticleActivity : BaseActivity() {

    @Autowired(name = "url")
    lateinit var url:String
    private lateinit var binding: ActivityArticleBinding

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityArticleBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ARouter.getInstance().inject(this)
        val web:WebView = binding.webView
        web.settings.javaScriptEnabled = true
        url.let { web.loadUrl(it) }
        web.webViewClient = object:WebViewClient(){
            override fun shouldOverrideKeyEvent(view: WebView?, event: KeyEvent?): Boolean {
                return false
            }

            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                if(!url.startsWith("http:")||!url.startsWith("https:")){
//                        val intent = Intent(
//                            Intent.ACTION_VIEW,
//                            Uri.parse(url)
//                        )
//                        startActivity(intent)
                    return true
                }
                url.let {
                    view?.loadUrl(it)
                }
                return false
            }
        }
    }
}